package com.GLM.Viewer.Circle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.GLM.Constants.Constant;
import com.GLM.Entity.RingEntity;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.google.gson.Gson;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class CircleSearchResultActivity extends Activity{
	private final static int CIRCLELIST_LOADING = 111;
	private final static int CIRCLELIST_LONDED = 222;

	private String[] itemKeys = new String[] { "msgNum", "fansNum", "circleName" };
	private int[] itemIds = new int[] { R.id.mycirclelistitem_message, R.id.myciclelistitem_fans, R.id.mycirclelistitem_name };

	private String searchStr;
	private ProgressBar mProgressBar;
	private ListView mListView;
	private SimpleAdapter mAdapter;
	private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();
	private List<RingEntity> allRings = new ArrayList<RingEntity>();

	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Thread thread;
			switch (msg.what) {
			case CIRCLELIST_LOADING:
				mProgressBar.setVisibility(View.VISIBLE);
				thread = new Thread(new Runnable() {
					public void run() {
						initSearchCircleList();
						mAdapter = new SimpleAdapter(CircleSearchResultActivity.this, mItems, R.layout.view_mycircles_listitem, itemKeys, itemIds);
						mHandler.sendEmptyMessage(CIRCLELIST_LONDED);
					}
				});
				thread.start();
				break;

			case CIRCLELIST_LONDED:
				mListView.setAdapter(mAdapter);
				mProgressBar.setVisibility(View.GONE);
				bindItemClickedEvent();
				break;
			}
		}
	};

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_circle_searchresult);
		mListView = (ListView)findViewById(R.id.search_result_listview);
		mProgressBar = (ProgressBar)findViewById(R.id.circlelist_progressbar);
		Intent intent = getIntent();
		searchStr = intent.getStringExtra("SearchStr");
		
		mHandler.sendEmptyMessage(CIRCLELIST_LOADING);
	}
	
	private void initSearchCircleList() {
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		try {
			jsonData.put(Constant.PARAM_CIRCLE_NAME, searchStr);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_GETSEARCHCIRCLE , jsonData);

			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					JSONArray jsonArray = data.getJSONArray(Constant.RESP_RING_DATA );
					initCircleListItemFromJSONObject(jsonArray);
				}
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	private void initCircleListItemFromJSONObject(JSONArray jsonArray) throws JSONException {
		Gson gson = new Gson();
		String msgNum = getString(R.string.mycircle_msgnumber);
		String fansNum = getString(R.string.mycircle_fansnumber);
		for (int i = 0; i < jsonArray.length(); i++) {
			RingEntity ringEntity = gson.fromJson(jsonArray.getJSONObject(i).toString(), RingEntity.class);
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("msgNum", msgNum + ringEntity.getMsg_count());
			item.put("fansNum", fansNum + ringEntity.getRing_user_list().length);
			item.put("circleName", ringEntity.getRing_name());
			mItems.add(item);
			allRings.add(ringEntity);
		}

	}

	private void bindItemClickedEvent() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ListView listView = (ListView) parent;
				Log.v("Test", "id = " + id + "(" + listView.getItemAtPosition(position).toString() + ")");
				RingEntity ringEntity = allRings.get((int) id);
				Log.d("Test", ringEntity.toString());
				Intent intent = new Intent(CircleSearchResultActivity.this, MyCirclesDetailActivity.class);
				intent.putExtra("ring", ringEntity);
				intent.putExtra("tempSource", 1);
				startActivity(intent);
			}
		});
	}
	
	public void gobackButton_OnClick(View v){
		NavUtils.navigateUpFromSameTask(CircleSearchResultActivity.this);
	}
}
